<?php
/**
 * create by breeze
 * Author: breeze
 * Date: 2021/3/27
 */
declare(strict_types=1);
namespace app\common\lib\sms;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class AliSmsOld
{
    public static function sendCode(string $phone, int $code) :bool {
        if(empty($phone) || empty($code)){
            return false;
        }
        AlibabaCloud::accessKeyClient(config("aliyun.access_key_id"), config("aliyun.access_secret"))
            ->regionId('cn-qingdao')
            ->asDefaultClient();
        $templateParam = [
            "code" => $code
        ];

        try {
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host(config("aliyun.host"))
                ->options([
                    'query' => [
                        'SignName' => config("aliyun.signName"),
                        'TemplateCode' => config("aliyun.templateCode"),
                        'TemplateParam' => json_encode($templateParam),
                        'PhoneNumbers' => $phone,
                    ],
                ])
                ->request();
            print_r($result->toArray());
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
            return false;
//            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
            return false;
//            echo $e->getErrorMessage() . PHP_EOL;
        }
        return true;
    }
}